#include <chrono>
#include <memory>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "std_srvs/srv/empty.hpp"

using namespace std::chrono_literals;

class ServiceClientNode : public rclcpp::Node
{
   public:
    ServiceClientNode() : Node("service_client_node")
    {
        // 创建客户端
        client_ = this->create_client<std_srvs::srv::Empty>("example_service");

        // 创建定时器，定期调用服务
        timer_ = this->create_wall_timer(5s, [this]() -> void {
            if (!client_->wait_for_service(1s))
            {
                RCLCPP_WARN(this->get_logger(), "Service not available");
                return;
            }

            auto request = std::make_shared<std_srvs::srv::Empty::Request>();
            auto future = client_->async_send_request(request);

            RCLCPP_INFO(this->get_logger(), "Calling service...");
        });

        RCLCPP_INFO(this->get_logger(), "Service client node started");
    }

   private:
    rclcpp::Client<std_srvs::srv::Empty>::SharedPtr client_;
    rclcpp::TimerBase::SharedPtr timer_;
};

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<ServiceClientNode>());
    rclcpp::shutdown();
    return 0;
}
